package neusoft.edu.cn.guanshangshiguang.activities;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import neusoft.edu.cn.guanshangshiguang.Listener.ListListener;
import neusoft.edu.cn.guanshangshiguang.Listener.OnItemClickListener;
import neusoft.edu.cn.guanshangshiguang.R;
import neusoft.edu.cn.guanshangshiguang.adapter.CommentSchoolAdapter;
import neusoft.edu.cn.guanshangshiguang.adapter.shenqingListAdapter;
import neusoft.edu.cn.guanshangshiguang.bean.CommentSchoolBean;
import neusoft.edu.cn.guanshangshiguang.bean.shenqingListBean;
import neusoft.edu.cn.guanshangshiguang.common.Common;
import neusoft.edu.cn.guanshangshiguang.model.CommentSchoolModel;
import neusoft.edu.cn.guanshangshiguang.model.shenqingListModel;

public class ChangeDetailsActivity extends AppCompatActivity {
    private List<shenqingListBean> Bean;
    shenqingListAdapter Adapter;
    private ImageView bpic;
    private TextView bname,uname,gender,school,old,copy,content,look,shoucang,shenqing,jiaohuan,num;
    private int shuaxin_flag=0;
    private int shujia_flag=0;
    private RecyclerView Recycler;
    SwipeRefreshLayout swipeRefreshLayout;

    ListListener listListener = new ListListener() {
        @Override
        public void onResponse(List List) {
            Bean = List;
            Adapter.setDate(Bean);
            if(shuaxin_flag==1){
                shuaxin_flag=0;
                swipeRefreshLayout.setRefreshing(false);
            }
            num.setText("申请列表("+Bean.size()+")");
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_SHORT).show();
        }
    };

    OnItemClickListener onItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Intent intent = new Intent(getApplicationContext(),shenqingListDetailsActivity.class);
            intent.putExtra("uname",Bean.get(position).getUname());
            intent.putExtra("school",Bean.get(position).getSchool());
            intent.putExtra("shenqing_bname",Bean.get(position).getShenqing_bname());
            intent.putExtra("gender",Bean.get(position).getGender());
            intent.putExtra("content",Bean.get(position).getContent());
            intent.putExtra("old",Bean.get(position).getOld());
            intent.putExtra("copy",Bean.get(position).getCopy());
            startActivity(intent);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();//去掉标题栏
        actionBar.hide();//去掉标题栏
        setContentView(R.layout.activity_change_details);
        init();
        initRecycler();

        Picasso.get()
                .load(Common.Address+getIntent().getStringExtra("pic"))
                .into(bpic);
        bname.setText(getIntent().getStringExtra("bname"));
        uname.setText(getIntent().getStringExtra("uname"));
        school.setText(getIntent().getStringExtra("school"));
        if(getIntent().getStringExtra("gender").toString().equals("1"))
            gender.setText("小哥哥");
        else gender.setText("小姐姐");
        look.setText(getIntent().getStringExtra("look")+"人收藏");
        if(getIntent().getStringExtra("copy").toString().equals("1"))
            copy.setText("正版");
        else if(getIntent().getStringExtra("copy").toString().equals("2"))
            copy.setText("珍藏");
        else copy.setText("独家");
        old.setText(getIntent().getStringExtra("old")+"成新");
        content.setText(getIntent().getStringExtra("content"));
        jiaohuan.setText(getIntent().getStringExtra("intention"));


        shoucang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(shujia_flag==0){
                    shujia_flag=1;
                    shoucang.setText("✔ 已收藏");
                }else{
                    shujia_flag=0;
                    shoucang.setText("+ 收藏");
                }

            }
        });

        shenqing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Intent intent = new Intent(getApplicationContext(),shenqingActivity.class);
                intent.putExtra("change_id",getIntent().getStringExtra("change_id"));
                startActivity(intent);
            }
        });

        shenqingListModel Model = new shenqingListModel();
        Model.getResultList(getIntent().getStringExtra("change_id"),listListener);
    }

    protected void onResume() {
        super.onResume();
        shenqingListModel Model = new shenqingListModel();
        Model.getResultList(getIntent().getStringExtra("change_id"),listListener);
    }

    private void init() {
        bname=findViewById(R.id.textView10);
        uname=findViewById(R.id.textView12);
        school=findViewById(R.id.textView15);
        gender=findViewById(R.id.textView17);
        look=findViewById(R.id.textView16);
        copy=findViewById(R.id.textView18);
        old=findViewById(R.id.textView19);
        content=findViewById(R.id.textView20);
        bpic=findViewById(R.id.imageView9);
        shoucang=findViewById(R.id.shoucang);
        shenqing=findViewById(R.id.shenqingjiaohuan);
        jiaohuan=findViewById(R.id.textView22);
        num=findViewById(R.id.textView23);
    }
    private void initRecycler() {
        Adapter = new shenqingListAdapter();

        Recycler = findViewById(R.id.recyclerView10);

        //布局管理器  可以设置显示数据的方式为垂直或者水平
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        //每个item如果是确定高度，设置此项提高性能
        Recycler.setHasFixedSize(true);

        swipeRefreshLayout = findViewById(R.id.swipeRefresh);

        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary),
                getResources().getColor(R.color.colorAccent),
                getResources().getColor(R.color.colorPrimaryDark));

        swipeRefreshLayout.setProgressViewOffset(true,50,60);

        swipeRefreshLayout.setRefreshing(false);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                shuaxin_flag=1;
                shenqingListModel Model = new shenqingListModel();
                Model.getResultList(getIntent().getStringExtra("change_id"),listListener);
            }
        });

        Recycler.setLayoutManager(linearLayoutManager);
        Adapter.setOnItemClickListener(onItemClickListener);
        Recycler.setAdapter(Adapter);

    }
}
